package DyingBaby.backend.sql;

import DyingBaby.backend.catalog.ColumnDef;

import java.util.List;
import java.util.Map;

/**
 * 抽象语法树的节点类
 * 表示解析后的 SQL 语句结构
 */
public class Ast {
    public interface Stmt {}

    //表示 CREATE TABLE 语句的 AST 节点，包含：
      //table: 表名
      //columns: 列定义列表（ColumnDef 对象）
      //primaryKey: 主键列名（可为 null）
    public static class CreateTable implements Stmt {
        public final String table;
        public final List<ColumnDef> columns;
        public final String primaryKey; // nullable
        public CreateTable(String table, List<ColumnDef> columns, String primaryKey) {
            this.table = table; this.columns = columns; this.primaryKey = primaryKey;
        }
    }

    //表示 INSERT 语句的 AST 节点，包含：
       //table: 表名
       //values: 列名到值的映射
    public static class Insert implements Stmt {
        public final String table;
        public final Map<String,Object> values;
        public Insert(String table, Map<String,Object> values) { this.table = table; this.values = values; }
    }
    //表示 SELECT 语句的 AST 节点，包含：
      //table: 表名
      //predicate: WHERE 条件表达式（可为 null
    public static class Select implements Stmt {
        public final String table;
        public final Expr predicate; // nullable
        public Select(String table, Expr predicate) { this.table = table; this.predicate = predicate; }
    }

    public static class Update implements Stmt {
        public final String table;
        public final Map<String,Object> set;
        public final Expr predicate; // nullable
        public Update(String table, Map<String,Object> set, Expr predicate) { this.table=table; this.set=set; this.predicate=predicate; }
    }

    public static class Delete implements Stmt {
        public final String table;
        public final Expr predicate; // nullable
        public Delete(String table, Expr predicate) { this.table = table; this.predicate = predicate; }
    }

    // Expressions
    public interface Expr {}
    // 等值比较
    public static class Eq implements Expr {
        public final String col;
        public final Object value;
        public Eq(String col, Object value) { this.col = col; this.value = value; }
    }
    // 不等比较
    public static class Ne implements Expr {
        public final String col;
        public final Object value;
        public Ne(String col, Object value) { this.col = col; this.value = value; }
    }

    // 大于比较
    public static class Gt implements Expr {
        public final String col;
        public final Object value;
        public Gt(String col, Object value) { this.col = col; this.value = value; }
    }

    // 小于比较
    public static class Lt implements Expr {
        public final String col;
        public final Object value;
        public Lt(String col, Object value) { this.col = col; this.value = value; }
    }

    // 大于等于比较
    public static class Ge implements Expr {
        public final String col;
        public final Object value;
        public Ge(String col, Object value) { this.col = col; this.value = value; }
    }

    // 小于等于比较
    public static class Le implements Expr {
        public final String col;
        public final Object value;
        public Le(String col, Object value) { this.col = col; this.value = value; }
    }

    // 逻辑与
    public static class And implements Expr {
        public final Expr left;
        public final Expr right;
        public And(Expr left, Expr right) { this.left = left; this.right = right; }
    }

    // 逻辑或
    public static class Or implements Expr {
        public final Expr left;
        public final Expr right;
        public Or(Expr left, Expr right) { this.left = left; this.right = right; }
    }

    // 逻辑非
    public static class Not implements Expr {
        public final Expr expr;
        public Not(Expr expr) { this.expr = expr; }
    }

    // 括号表达式
    public static class Paren implements Expr {
        public final Expr expr;
        public Paren(Expr expr) { this.expr = expr; }
    }
} 